package com.shen.nettysocketchat

import android.content.Context
import com.shen.library_nettysocket.protobuf.MessageProtobuf.Msg
import android.net.ConnectivityManager
import android.net.NetworkInfo
import androidx.core.content.ContextCompat.getSystemService
import com.alibaba.fastjson.JSONObject
import com.google.protobuf.Extension
import com.shen.library_nettysocket.listener.OnEventListener
import com.shen.library_nettysocket.protobuf.MessageProtobuf
import java.util.*

/**
 * 与ims交互的listener
 */
class IMSEventListener(private val userId: String, private val token: String) : OnEventListener {
    /**
     * 接收ims [msg]转发过来的消息
     */
    override fun dispatchMsg(msg: Msg?) {
        if(msg != null)
            MessageProcessor.instance.receiveMsg(MessageBuilder.getMessageByProtobuf(msg))
    }

    /**
     * 「网络」是否可用
     */
    override val isNetworkAvailable: Boolean
        get() {
            val cm = NettyChatApp.sharedInstance()?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
            val info = cm?.activeNetworkInfo
            return info != null && info.isConnected
        }

    /** 设置ims「重连间隔」时长，「0」表示「默认使用ims的值」 */
    override val reconnectInterval: Int = 0
    /** 设置ims「连接超时」时长，「0」表示「默认使用ims的值」 */
    override val connectTimeout: Int = 0
    /** 设置「应用在前台」时ims「心跳间隔」时长，「0」表示「默认使用ims的值」*/
    override val foregroundHeartbeatInterval: Int = 0
    /** 设置「应用在后台」时ims「心跳间隔」时长，「0」表示「默认使用ims的值」 */
    override val backgroundHeartbeatInterval: Int = 0
    /** 获取 应用层「消息·发送·状态报告」消息类型 */
    override val serverSentReportMsgType: Int
        get() = MessageType.SERVER_MSG_SENT_STATUS_REPORT.msgType
    /** 获取 应用层「消息·接收·状态报告」消息类型  */
    override val clientReceivedReportMsgType: Int
        get() = MessageType.CLIENT_MSG_RECEIVED_STATUS_REPORT.msgType
    /** 设置ims「消息发送超时」重发次数，「0」表示「默认使用ims的值」 */
    override val resendCount: Int = 0
    /** 设置ims「消息发送超时」重发间隔时长，「0」表示「默认使用ims的值」 */
    override val resendInterval: Int = 0

    /**
     * 构建「握手消息」
     *
     * @return
     */
    override val handshakeMsg: Msg
        get() {
            val builder = Msg.newBuilder()
            val headBuilder = MessageProtobuf.Head.newBuilder()
            headBuilder.msgId = UUID.randomUUID().toString()
            headBuilder.msgType = MessageType.HANDSHAKE.msgType
            headBuilder.fromId = userId
            headBuilder.timestamp = System.currentTimeMillis()
            val jsonObj = JSONObject()
            jsonObj["token"] = token
            headBuilder.extend = jsonObj.toString()
            builder.head = headBuilder.build()
            return builder.build()
        }

    /**
     * 构建「心跳消息」
     *
     * @return
     */
    override val heartbeatMsg: Msg
        get() {
            val builder = Msg.newBuilder()
            val headBuilder = MessageProtobuf.Head.newBuilder()
            headBuilder.msgId = UUID.randomUUID().toString()
            headBuilder.msgType = MessageType.HEARTBEAT.msgType
            headBuilder.fromId = userId
            headBuilder.timestamp = System.currentTimeMillis()
            builder.head = headBuilder.build()
            return builder.build()
        }

}